/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mapbox.ohos.gestures;

import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

import com.mapbox.ohos.gestures.ohan.GestureDetector;

import static com.mapbox.ohos.gestures.OhosGesturesManager.GESTURE_TYPE_DOUBLE_TAP;
import static com.mapbox.ohos.gestures.OhosGesturesManager.GESTURE_TYPE_DOUBLE_TAP_EVENT;
import static com.mapbox.ohos.gestures.OhosGesturesManager.GESTURE_TYPE_DOWN;
import static com.mapbox.ohos.gestures.OhosGesturesManager.GESTURE_TYPE_FLING;
import static com.mapbox.ohos.gestures.OhosGesturesManager.GESTURE_TYPE_LONG_PRESS;
import static com.mapbox.ohos.gestures.OhosGesturesManager.GESTURE_TYPE_SCROLL;
import static com.mapbox.ohos.gestures.OhosGesturesManager.GESTURE_TYPE_SHOW_PRESS;
import static com.mapbox.ohos.gestures.OhosGesturesManager.GESTURE_TYPE_SINGLE_TAP_CONFIRMED;
import static com.mapbox.ohos.gestures.OhosGesturesManager.GESTURE_TYPE_SINGLE_TAP_UP;

/**
 * Detector that wraps {@link GestureDetector}.
 */
public class StandardGestureDetector extends BaseGesture<StandardGestureDetector.StandardOnGestureListener> {

    private final GestureDetector GESTURE_DETECTOR;

    public StandardGestureDetector(Context context, OhosGesturesManager ohosGesturesManager) {
        super(context, ohosGesturesManager);

        this.GESTURE_DETECTOR = new GestureDetector(context, innerListener);
    }

    final StandardOnGestureListener innerListener = new StandardOnGestureListener() {

        @Override
        public boolean onSingleTapUp(TouchEvent touchEvent) {
            return canExecute(GESTURE_TYPE_SINGLE_TAP_UP) && listener.onSingleTapUp(touchEvent);
        }

        @Override
        public void onLongPress(TouchEvent touchEvent) {
            if (canExecute(GESTURE_TYPE_LONG_PRESS)) {
                listener.onLongPress(touchEvent);
            }
        }

        @Override
        public boolean onScroll(TouchEvent e1, TouchEvent e2, float distanceX, float distanceY) {
            return canExecute(GESTURE_TYPE_SCROLL) && listener.onScroll(e1, e2, distanceX, distanceY);
        }

        @Override
        public boolean onFling(TouchEvent e1, TouchEvent e2, float velocityX, float velocityY) {
            return canExecute(GESTURE_TYPE_FLING) && listener.onFling(e1, e2, velocityX, velocityY);
        }

        @Override
        public void onShowPress(TouchEvent touchEvent) {
            if (canExecute(GESTURE_TYPE_SHOW_PRESS)) {
                listener.onShowPress(touchEvent);
            }
        }

        @Override
        public boolean onDown(TouchEvent touchEvent) {
            return canExecute(GESTURE_TYPE_DOWN) && listener.onDown(touchEvent);
        }

        @Override
        public boolean onDoubleTap(TouchEvent touchEvent) {
            return canExecute(GESTURE_TYPE_DOUBLE_TAP) && listener.onDoubleTap(touchEvent);
        }

        @Override
        public boolean onDoubleTapEvent(TouchEvent touchEvent) {
            return canExecute(GESTURE_TYPE_DOUBLE_TAP_EVENT) && listener.onDoubleTapEvent(touchEvent);
        }

        @Override
        public boolean onSingleTapConfirmed(TouchEvent touchEvent) {
            return canExecute(GESTURE_TYPE_SINGLE_TAP_CONFIRMED) && listener.onSingleTapConfirmed(touchEvent);
        }
    };

    /**
     * StandardOnGestureListener
     */
    public interface StandardOnGestureListener extends GestureDetector.OnGestureListener,
        GestureDetector.OnDoubleTapListener {
    }

    /**
     * SimpleStandardOnGestureListener
     *
     * @since 2021-07-28
     */
    public static class SimpleStandardOnGestureListener implements StandardOnGestureListener {
        @Override
        public boolean onSingleTapConfirmed(TouchEvent touchEvent) {
            return false;
        }

        @Override
        public boolean onDoubleTap(TouchEvent touchEvent) {
            return false;
        }

        @Override
        public boolean onDoubleTapEvent(TouchEvent touchEvent) {
            return false;
        }

        @Override
        public boolean onDown(TouchEvent touchEvent) {
            return false;
        }

        @Override
        public void onShowPress(TouchEvent touchEvent) {

        }

        @Override
        public boolean onSingleTapUp(TouchEvent touchEvent) {
            return false;
        }

        @Override
        public boolean onScroll(TouchEvent e1, TouchEvent e2, float distanceX, float distanceY) {
            return false;
        }

        @Override
        public void onLongPress(TouchEvent touchEvent) {

        }

        @Override
        public boolean onFling(TouchEvent e1, TouchEvent e2, float velocityX, float velocityY) {
            return false;
        }
    }

    @Override
    protected boolean analyzeEvent(TouchEvent motionEvent) {
        return GESTURE_DETECTOR.onTouchEvent(motionEvent);
    }

    /**
     * isLongpressEnabled
     *
     * @return boolean
     */
    public boolean isLongpressEnabled() {
        return GESTURE_DETECTOR.isLongpressEnabled();
    }

    /**
     * setIsLongpressEnabled
     *
     * @param enabled
     */
    public void setIsLongpressEnabled(boolean enabled) {
        GESTURE_DETECTOR.setIsLongpressEnabled(enabled);
    }
}
